package com.hualude.roadsign.repository;

import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.PortRedRuleDto;
import com.hualude.roadsign.model.PortRedRuleEntity;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

/**
 * Created by xu_jiaHui on 2017/12/9.
 */
public interface PortRedRuleRepository extends CrudRepository<PortRedRuleEntity, Integer> {
    /**
     * @Description: 获取常规规则用于DT展示
     * @author xu_jiaHui
     * @date 2017/12/9 16:28
     */
    public List<PortRedRuleDto> getPortRedRule4DT(DTRequestDto dtRequestDto, Integer alarmId);

    /**
     * @Description:
     * @author xu_jiaHui
     * @date 2017/12/9 16:29
     */
    @Query(value = "select count(*) from PortRedRuleEntity where alarmId=?1")
    public Integer getPortRedRuleCount4DT(Integer alarmId);

    @Modifying
    @Query(value = "update PortRedRuleEntity set redRuleMark=?1 where ruleModelId=?2")
    public void updateRuleMark(String greenTime, Integer ruleModelId) throws Exception;

    /**
     * @Description: 获取100规则的数量
     * @author xu_jiaHui
     * @date 2017/12/11 10:22
     */
    @Query(value = "select count(*) from PortRedRuleEntity where alarmId=?1 and redRuleFlag=100")
    public Integer getRedIooRuleCount(Integer alarmId) throws Exception;

    /**
     * @Description: 获取200规则的数量
     * @author xu_jiaHui
     * @date 2017/12/11 10:22
     */
    @Query(value = "select count(*) from PortRedRuleEntity where alarmId=?1 and redRuleFlag=200")
    public Integer getRedZooRuleCount(Integer alarmId) throws Exception;

    /**
     * @Description:
     * @author weiyuxian
     * @date 2018/4/24 20:28
     */
    @Modifying
    @Query(value = "delete from PortRedRuleEntity where ruleModelId=?1")
    public Integer deletePortRedRuleByRuleModelId(Integer ruleModelId) throws Exception;

    public List<PortRedRuleDto> getRedRuleByModelId(DTRequestDto dtRequestDto, Integer modelId) throws Exception;

    public Integer getRedRuleCountByModelId(DTRequestDto dtRequestDto, Integer modelId) throws Exception;

    @Query(value = "from PortRedRuleEntity where ruleModelId=?1 and redRuleFlag=100  ORDER BY redRuleLevel ")
    public List<PortRedRuleEntity> get100RedRules(Integer ruleModelId) throws Exception;

    @Query(value = "from PortRedRuleEntity where ruleModelId=?1 and redRuleFlag=200  ORDER BY redRuleLevel ")
    public List<PortRedRuleEntity> get200RedRules(Integer ruleModelId) throws Exception;

    @Modifying
    @Query(value = "update PortRedRuleEntity set redRuleLevel=?2,redRuleContent=?3,redRuleStatus=?4,redRuleTag=?5,redRuleFlag=?6 where redRuleId=?1")
    Integer updateRule(Integer redRuleId, Integer redRuleLevel, String redRuleContent, String redRuleStatus, String redRuleTag,String redRuleFlag);
}
